﻿using System;
using System.Collections.Generic;
using ActiproSoftware.Text;
using ActiproSoftware.Text.Parsing;
using ActiproSoftware.Text.Parsing.LLParser;
using ActiproSoftware.Text.Utility;
using ActiproSoftware.Windows.Controls.SyntaxEditor;
using ActiproSoftware.Windows.Controls.SyntaxEditor.IntelliPrompt;
using ActiproSoftware.Windows.Controls.SyntaxEditor.IntelliPrompt.Implementation;
using ActiproSoftware.Text.Languages.CSharp.Implementation;
using CodeEditorTest;
using ActiproSoftware.Text.Languages.DotNet;

namespace CodeEditorTest
{

    /// <summary>
    /// Provides IntelliPrompt completion data for the <c>Simple</c> language.
    /// </summary>
    public class QueryCompletionProvider : CSharpCompletionProvider
    {

        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // OBJECT
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Initializes a new instance of the <c>SimpleCompletionProvider</c> class.
        /// </summary>
        public QueryCompletionProvider()
        { 
        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////
        // PUBLIC PROCEDURES
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Requests that an <see cref="ICompletionSession"/> be opened for the specified <see cref="IEditorView"/>.
        /// </summary>
        /// <param name="view">The <see cref="IEditorView"/> that will host the session.</param>
        /// <param name="canCommitWithoutPopup">Whether the session can immediately commit if a single match is made when the session is opened, commonly known as "complete word" functionality.</param>
        /// <returns>
        /// <c>true</c> if a session was opened; otherwise, <c>false</c>.
        /// </returns>
        public override bool RequestSession(IEditorView view, bool canCommitWithoutPopup)
        {
            // Get the context factory service
            CSharpContextFactory contextFactory = view.SyntaxEditor.Document.Language.GetService<CSharpContextFactory>();
            if (contextFactory != null)
            {
                // Get a context
                IDotNetContext context = contextFactory.CreateContext(view.Selection.EndSnapshotOffset);

                // Create a session
                CompletionSession session = new CompletionSession();
                session.CanCommitWithoutPopup = canCommitWithoutPopup;

                //switch (context.ContainingAstNode)
                //{
                //    case SimpleContextType.Default:
                //        // Add items for keywords
                //        session.Items.Add(new CompletionItem("function", new CommonImageSourceProvider(CommonImage.Keyword),
                //            new PlainTextContentProvider("Declares a function.")));
                //        break;
                //    case SimpleContextType.FunctionDeclarationBlock:
                //    case SimpleContextType.FunctionReference:
                //        {
                //            // Add items for keywords
                //            session.Items.Add(new CompletionItem("var", new CommonImageSourceProvider(CommonImage.Keyword),
                //                new PlainTextContentProvider("Declares a variable.")));
                //            session.Items.Add(new CompletionItem("return", new CommonImageSourceProvider(CommonImage.Keyword),
                //                new PlainTextContentProvider("Returns a value.")));

                //            // Add items (one for each function name)
                //            ILLParseData parseData = view.SyntaxEditor.Document.ParseData as ILLParseData;
                //            if ((parseData != null) && (parseData.Ast != null))
                //            {
                //                // Loop through the AST nodes 
                //                foreach (IAstNode functionAstNode in parseData.Ast.Children)
                //                {
                //                    // If the child node is a function declaration with children...
                //                    if ((functionAstNode.Value == "FunctionDeclaration") && (functionAstNode.HasChildren))
                //                    {
                //                        IAstNode nameAstNode = functionAstNode.Children[0];

                //                        session.Items.Add(new CompletionItem(nameAstNode.Value, new CommonImageSourceProvider(CommonImage.MethodPublic),
                //                            new HtmlContentProvider(String.Format("<span style=\"color: blue;\">function</span> {0}", nameAstNode.Value))));
                //                    }
                //                }
                //            }
                //            break;
                //        }
                //}

                if (session.Items.Count > 0)
                {
                    // Ensure the items are sorted and open the session
                    session.SortItems();
                    session.Open(view);
                    return true;
                }
            }
            return false;
        }

    }

}

